<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//信用卡管理表
class Credit_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->credit = 'credit';
        $this->credit_category = 'credit_category';
    }
    
    function get($where){
        $this->db->select($this->credit.'.*');
        $this->db->from($this->credit);
        $this->db->where($where);
        $this->db->order_by('id desc');
        $query = $this->db->get();
        return $query->row_array(); 
    }
    
    function get_credit_count($where){
        $this->db->select($this->credit.'.*');
        $this->db->from($this->credit);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function get_credit_list($where,$page,$limit){
        $this->db->select($this->credit.'.*,c1.title bank_title,c2.title use_title,c3.title degree_title,c4.title coin_title,c5.title organ_title');
        $this->db->from($this->credit);
        $this->db->join("tan_credit_category c1","c1.id = ".$this->credit.".bank_type","left");
        $this->db->join("tan_credit_category c2","c2.id = ".$this->credit.".use_type","left");
        $this->db->join("tan_credit_category c3","c3.id = ".$this->credit.".degree_type","left");
        $this->db->join("tan_credit_category c4","c4.id = ".$this->credit.".coin_type","left");
        $this->db->join("tan_credit_category c5","c5.id = ".$this->credit.".organ_type","left");
        $this->db->where($where);
        $this->db->limit($limit, $page);
        $this->db->order_by('sort asc');
        $query = $this->db->get();
        return $query->result_array(); 
    }
    
    function getAllCategory(){
        $this->db->select('*');
        $this->db->from($this->credit_category);
        $this->db->where('is_hide','1');
        $this->db->order_by('id asc');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    function get_credit_category($where){
        $this->db->select($this->credit_category.'.*');
        $this->db->from($this->credit_category);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->row_array(); 
    }
    
    
    function get_credit_category_count($where){
        $this->db->select($this->credit_category.'.*');
        $this->db->from($this->credit_category);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function get_credit_category_list($where,$page,$limit){
        $this->db->select($this->credit_category.'.*');
        $this->db->from($this->credit_category);
        $this->db->where($where);
        $this->db->limit($limit, $page);
        $query = $this->db->get();
        return $query->result_array(); 
    }
    //插入信用卡类型
    function insert_credit($data)
    {
        $this->db->insert($this->credit, $data);
        if($this->db->affected_rows()){
           return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    //插入信用卡类型
    function insert_credit_category($data)
    {
        $this->db->insert($this->credit_category, $data);
        if($this->db->affected_rows()){
           return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    //更新
    function update_credit($data,$id)
    {
            return $this->db->where('id',$id)->update($this->credit, $data);
    }
    
    //更新类型
    function update_category($data,$category_id)
    {
            return $this->db->where('id',$category_id)->update($this->credit_category, $data);
    }
    
    //删除类型
    function delete_category($delid){
        $this->db->where('id',$delid);
        $this->db->delete($this->credit_category);
        return $this->db->affected_rows();
    }
    //递归生成树形数组
    function tree($table,$pid=0){
        $tree = array();
        foreach($table as $row){
            if($row['pid']==$pid){
                $tmp = $this->tree($table,$row['id']);
                if($tmp){
                        $row['children']=$tmp;
                }else{
                        $row['leaf'] = true;
                }
                $tree[]=$row;                
            }
        }
        return $tree;
    }
}
